package org.eclipse.iota.ui;

import org.eclipse.emf.common.ui.URIEditorInput;
import org.eclipse.emf.common.util.URI;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IEditorDescriptor;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

/**
 * The activator class controls the plug-in life cycle.
 * @generated NOT
 */
public class IotaUIPlugin extends AbstractUIPlugin {

	// The plug-in ID.
	public static final String ID = "org.eclipse.iota.ui";
	
	// The shared instance.
	private static IotaUIPlugin plugin;
	
	// Images IDs.
	public static final int IMAGE_PRIORITY_VERY_LOW = 0;
	public static final int IMAGE_PRIORITY_LOW = 1;
	public static final int IMAGE_PRIORITY_NORMAL = 2;
	public static final int IMAGE_PRIORITY_HIGH = 3;
	public static final int IMAGE_PRIORITY_VERY_HIGH = 4;
	
	// Images.
	private Image[] images;
	
	/**
	 * Constructor.
	 */
	public IotaUIPlugin() {
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}

	/**
	 * Returns the shared instance.
	 * @return the shared instance.
	 */
	public static IotaUIPlugin getInstance() {
		return plugin;
	}

	/**
	 * Returns an image descriptor for the image file at the given
	 * plug-in relative path.
	 * @param path the path.
	 * @return the image descriptor.
	 */
	public static ImageDescriptor getImageDescriptor(String path) {
		return imageDescriptorFromPlugin(ID, path);
	}
	
	public Image getImage(int id) {
		if (images==null) {
			images = new Image[] {
				getImageDescriptor("icons/very_low.gif").createImage(),
				getImageDescriptor("icons/low.gif").createImage(),
				getImageDescriptor("icons/normal.gif").createImage(),
				getImageDescriptor("icons/high.gif").createImage(),
				getImageDescriptor("icons/very_high.gif").createImage()	
			};
		}
		if (id>=0 && id<images.length) {
			return images[id];
		} else {
			return null;
		}
	}
	
	public static boolean openEditor(IWorkbench workbench, URI fileURI) {
		IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
		IWorkbenchPage page = workbenchWindow.getActivePage();
		IEditorDescriptor editorDescriptor = workbench.getEditorRegistry()
				.getDefaultEditor(fileURI.toFileString());
		if (editorDescriptor == null) {
			MessageDialog.openError(workbenchWindow.getShell(),"Open file", "Error opening file: " + fileURI);
			return false;
		} else {
			try {
				page.openEditor(new URIEditorInput(fileURI), editorDescriptor.getId());
			} catch (PartInitException exception) {
				MessageDialog.openError(workbenchWindow.getShell(), "Open file", exception.getMessage());
				return false;
			}
		}
		return true;
	}


}
